package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/24.
 */
@Data
public class Harmless implements Serializable{

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = -7409951452999076088L;

    /**
     * 主键id
     */
    private Long id;

    /**
     * 无害化单据编号
     */
    private String number;

    /**
     * 检疫申报点
     */
    private Long applyReportId;

    /**
     * 申报点名称
     */
    private String applyReportName;

    /**
     * 屠宰点
     */
    private Long butcherFactoryId;

    /**
     * 屠宰场名称
     */
    private String butcherFactoryName;

    /**
     * 关联产生无害化对应的单据ID
     */
    private Long reportId;

    /**
     * 关联单据编号
     */
    private String reportNumber;

    /**
     * 关联单据或单据状态类型
     */
    private Integer reportType;

    /**
     * 动物种类
     */
    private Integer animalType;

    /**
     * 产品种类(同步检疫)
     */
    private String productType;

    /**
     * 单位
     */
    private Integer unit;

    /**
     * 数量
     */
    private Float quantity;

    /**
     * 处理方式
     */
    private Integer treatment;

    /**
     * 备注
     */
    private String remark;

    /**
     * 检疫员id
     */
    private Long quarantineUserId;

    /**
     * 检疫员name
     */
    private String quarantineUserName;

    /**
     * 养殖场id
     */
    private Long breedFactoryId;

    /**
     * 养殖场名称
     */
    private String breedFactoryName;

    /**
     * 厂方代表id
     */
    private Long factoryOwnerId;

    /**
     * 厂方代表
     */
    private String factoryOwnerName;

    /**
     *
     */
    private Map<String ,Map<String ,String>> photosJsonMap;

    /**
     * 无害化照片
     */
    @JsonIgnore
    private String photosJson;

    /**
     *
     */
    private Map<String ,String> videoJsonMap;

    /**
     * 无害化视频
     */
    @JsonIgnore
    private String videoJson;

    /**
     *
     */
    private Map<String ,Map<String ,String>> videoSnapshootJsonMap;

    /**
     * 视频快照
     */
    @JsonIgnore
    private String videoSnapshootJson;

    /**
     * 耳标号
     */
    @JsonIgnore
    private String earTagNumber;


    private List<String> earTagNumbers;

    /**
     * 是否本地
     */
    private Integer isLocal;

    /**
     * 状态 0：未提交 1：待处理 2：完成
     */
    private Integer status;

    /**
     * 单据标志位 1：正常，-1：删除
     */
    private Integer DeleteFlag;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;


    public void setPhotosJson(String photosJson){
        this.photosJson = photosJson;

        if(Strings.isNullOrEmpty(photosJson)){
            this.photosJsonMap = Maps.newHashMap();
        }else {
            try {
                this.photosJsonMap = objectMapper.readValue(photosJson, new TypeReference<Map<String ,Map<String , String>>>() {});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

    public void setPhotosJsonMap(Map<String , Map<String , String>> photosJsonMap){
        this.photosJsonMap = photosJsonMap;

        if(photosJsonMap == null || photosJsonMap.isEmpty()){
            this.photosJson = null;
        }else {
            try {
                this.photosJson = objectMapper.writeValueAsString(photosJsonMap);
            } catch (JsonProcessingException e) {
                //ignore this fuck JsonProcessingException
            }
        }
    }

    public void setVideoJson(String videoJson){
        this.videoJson = videoJson;
        if(Strings.isNullOrEmpty(videoJson)){
            this.videoJsonMap = Maps.newHashMap();
        }else {
            try {
                this.videoJsonMap = objectMapper.readValue(videoJson, new TypeReference<Map<String , String>>() {});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

    public void setVideoJsonMap(Map<String,String> videoJsonMap)  {
        this.videoJsonMap = videoJsonMap;

        if(videoJsonMap == null || videoJsonMap.isEmpty()){
            this.videoJson = null;
        }else {
            try {
                this.videoJson = objectMapper.writeValueAsString(videoJsonMap);
            } catch (JsonProcessingException e) {
                //ignore this fuck JsonProcessingException
            }
        }
    }

    public void setVideoSnapshootJson(String videoSnapshootJson){
        this.videoSnapshootJson = videoSnapshootJson;

        if(Strings.isNullOrEmpty(videoSnapshootJson)){
            this.videoSnapshootJsonMap = Maps.newHashMap();
        }else {
            try {
                this.videoSnapshootJsonMap = objectMapper.readValue(videoSnapshootJson, new TypeReference<Map<String , Map<String , String>>>() {});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

    public void setVideoSnapshootJsonMap(Map<String , Map<String , String>> videoSnapshootJsonMap){
        this.videoSnapshootJsonMap = videoSnapshootJsonMap;

        if(videoSnapshootJsonMap == null || videoSnapshootJsonMap.isEmpty()){
            this.videoSnapshootJson = null;
        }else {
            try {
                this.videoSnapshootJson = objectMapper.writeValueAsString(videoSnapshootJsonMap);
            } catch (JsonProcessingException e) {
                //ignore this fuck JsonProcessingException
            }
        }
    }

    public void setEarTagNumber(String earTagNumber){
        this.earTagNumber = earTagNumber;

        if(Strings.isNullOrEmpty(earTagNumber)){
            this.earTagNumbers = new ArrayList<>();
        }else {
            try {
                this.earTagNumbers =
                        objectMapper.readValue(earTagNumber, new TypeReference<List<String>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }

    public void setEarTagNumbers(List<String> earTagNumbers){
        this.earTagNumbers = earTagNumbers;

        if(earTagNumbers == null || earTagNumbers.isEmpty()){
            this.earTagNumber = null;
        }else {
            try {
                this.earTagNumber = objectMapper.writeValueAsString(earTagNumbers);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }
}
